package com.bajie.bjlisten.utils;

import com.bajie.bjlisten.dto.UserConstants;
import com.bajie.bjlisten.entity.BJMenuChild;
import com.bajie.bjlisten.entity.BjUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.util.List;

/**
 * 获取当前的登陆人以及当前登陆人的权限
 */
public class UserUtil {
    public static BjUser getCurrentUser() {
        BjUser user = (BjUser) getSession().getAttribute(UserConstants.LOGIN_USER);

        return user;
    }

    public static void setUserSession(BjUser user) {
        getSession().setAttribute(UserConstants.LOGIN_USER, user);
    }

    @SuppressWarnings("unchecked")
    public static List<BJMenuChild> getCurrentPermissions() {
        List<BJMenuChild> list = (List<BJMenuChild>) getSession().getAttribute(UserConstants.USER_PERMISSIONS);

        return list;
    }

    public static void setPermissionSession(List<BJMenuChild> list) {
        getSession().setAttribute(UserConstants.USER_PERMISSIONS, list);
    }

    public static Session getSession() {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();

        return session;
    }
}
